package ru.evga.gwt.lib.common.client.ui.dialog;

import ru.evga.gwt.lib.common.client.domain.BaseEntityDto;
import ru.evga.gwt.lib.common.client.ui.editor.DtoEditManager;
import ru.evga.gwt.lib.common.client.ui.editor.IEntityEditor;
import ru.evga.gwt.lib.common.client.ui.editor.ISaveEntityCallback;

import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;


public class AbstractDtoEditDialog<T extends BaseEntityDto, E extends IEntityEditor<T>>
    extends AbstractDialog
    implements EditButtonBar.Presenter, ISaveEntityCallback<T>
    
{
    
    private DtoEditManager<T, E> dtoEditManager;
    private ISaveEntityCallback<T> saveEntityCallback;
    public AbstractDtoEditDialog()
    {
        super(true);
        dtoEditManager = new DtoEditManager<T, E>();
    }
    
    
    protected void init(T data, E editor, ISaveEntityCallback<T> saveEntityCallback, SimpleBeanEditorDriver<T, E> driver, String entityName)
    {
        this.saveEntityCallback = saveEntityCallback;
        dtoEditManager.init(data, editor, this, driver, entityName);
    }


    @Override
    Widget createButtonBar()
    {
        EditButtonBar bar = new EditButtonBar();
        bar.setPresenter(this);
        return bar;
    }


    @Override
    IsWidget createContent()
    {
        return dtoEditManager.createContent();
    }


    public void saveClicked()
    {
        dtoEditManager.save();
    }


    public void cancelClicked()
    {
        dtoEditManager.cancel();
        close();
    }


    public void onEntitySaved(T savedEntity)
    {
        if (saveEntityCallback != null)
        {
            saveEntityCallback.onEntitySaved(savedEntity);
        }
        close();
    }

}
